package com.mdlib.helper.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.help.R;
import com.mdlib.helper.event.TimeEvent;
import com.mdlib.helper.model.entity.TimeEntity;
import com.mdlib.helper.ui.adapter.TextAdapter;
import com.mdlib.helper.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/21 10:59
 * 邮箱：hqy_xz@126.com
 */

public class SuccessTimeDialogFragment extends DialogFragment {


    @Bind(R.id.tv_month_time)
    TextView mTvMonthTime;
    @Bind(R.id.tv_hour_time)
    TextView mTvHourTime;
    @Bind(R.id.rv_month_time)
    RecyclerView mRvMonthTime;
    @Bind(R.id.rv_hour_time)
    RecyclerView mRvHourTime;
    @Bind(R.id.tv_success_time_submit)
    TextView mTvSuccessTimeSubmit;
    @Bind(R.id.iv_service_money_bg)
    ImageView mIvServiceMoneyBg;
    @Bind(R.id.rl_minnetonka_service_money)
    RelativeLayout mRlMinnetonkaServiceMoney;
    @Bind(R.id.ll_success_time_close)
    LinearLayout mLlSuccessTimeClose;

    private List<TimeEntity> monthList = new ArrayList<>();
    private List<TimeEntity> hourList = new ArrayList<>();

    private TextAdapter mMonthTextAdapter;
    private TextAdapter mHourTextAdapter;

    private TimeEntity monthTime;

    private TimeEntity hourTime;

    private String[] hour = {"1:00", "2:00", "3:00", "4:00", "5:00", "6:00", "7:00", "8:00"
            , "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00"
            , "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00", "24:00"};

    public static SuccessTimeDialogFragment newInstance() {

        return new SuccessTimeDialogFragment();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Dialog_Share);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_success_time, null);
        builder.setView(view);
        ButterKnife.bind(this, view);
        init();
        return builder.create();
    }

    @Override
    public void onStart() {
        super.onStart();

        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    public void init() {
        getMonth();
        getHour();

        //月份
        mMonthTextAdapter = new TextAdapter(monthList);
        mRvMonthTime.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvMonthTime.setAdapter(mMonthTextAdapter);
        mRvMonthTime.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (monthTime != null) {
                    monthTime.setSelected(false);
                }
                TimeEntity month = monthList.get(position);

                if (month == monthTime) {
                    if (month.isSelected()) {
                        month.setSelected(true);
                    }
                }
                mTvMonthTime.setText(month.getTime());
                month.setSelected(true);
                monthTime = month;
                mMonthTextAdapter.notifyDataSetChanged();

            }
        });

        //时间
        mHourTextAdapter = new TextAdapter(hourList);
        mRvHourTime.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvHourTime.setAdapter(mHourTextAdapter);
        mRvHourTime.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (hourTime != null) {
                    hourTime.setSelected(false);
                }
                TimeEntity hour = hourList.get(position);
                if (hour == hourTime) {
                    if (hour.isSelected()) {
                        hour.setSelected(true);
                    }
                }
                mTvHourTime.setText(hour.getTime());
                hour.setSelected(true);
                hourTime = hour;
                mHourTextAdapter.notifyDataSetChanged();

            }
        });
    }

    private void getMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd");
        Calendar claendar = Calendar.getInstance();
        for (int i = 0; i < 3; i++) {
            TimeEntity month = new TimeEntity();
            if (i == 0) {
                claendar.add(Calendar.DATE, i);
            } else {
                claendar.add(Calendar.DATE, 1);
            }
            month.setId(i);
            month.setTime(sdf.format(claendar.getTime()));
            monthList.add(month);
        }
    }

    private void getHour() {
        for (int i = 0; i < hour.length; i++) {
            TimeEntity time = new TimeEntity();
            time.setId(i);
            time.setTime(hour[i]);
            hourList.add(time);
        }
    }

    @OnClick({R.id.tv_success_time_submit, R.id.ll_success_time_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_success_time_submit:
                String month = mTvMonthTime.getText().toString();
                String hour = mTvHourTime.getText().toString();
                if (!month.equals("0") && !hour.equals("0")) {
                    EventBus.getDefault().post(new TimeEvent(month, hour));
                    dismiss();
                } else {
                    ToastUtil.showToast("请选择时间!");
                }
                break;
            case R.id.ll_success_time_close:
                dismiss();
                break;
        }
    }
}
